package org.example.ag_notes.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.ag_notes.dto.article.TagDTO;
import org.example.ag_notes.dto.response.Response;
import org.example.ag_notes.entity.Article;
import org.example.ag_notes.exception.AuthenticationException;
import org.example.ag_notes.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


//TODO 修改返回体字段
@Tag(name = "文章管理" , description = "文章增删改查")
@RestController
@RequestMapping("/api/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    //添加文章接口
    @PostMapping("add")
    public ResponseEntity<?> addArticle(@RequestBody Article article , @RequestHeader("Authorization") String accessToken) {
        try{
            return new ResponseEntity<>(Response.success("添加成功" , articleService.addArticle(article , accessToken)), HttpStatus.OK);
        }catch (Exception e){
            return ResponseEntity.internalServerError().body(
                    Response.error(500, "服务器内部错误")
            );
        }
    }

    //获取文章接口
    @GetMapping("get")
    public ResponseEntity<?> getArticleByUser(@RequestHeader("Authorization") String accessToken) {
        System.err.println("调用了获取接口");
        try{
            return new ResponseEntity<>(Response.success("获取成功" , articleService.getArticlesByUser(accessToken)), HttpStatus.OK);
        }catch (Exception e){
            return ResponseEntity.internalServerError().body(
                    Response.error(500, "服务器内部错误")
            );
        }
    }


    //更新文章接口
    @PutMapping("update")
    public ResponseEntity<?> updateArticle(@RequestBody Article article , @RequestHeader("Authorization") String accessToken)
    {
        try{
            return new ResponseEntity<>(Response.success("更新成功" , articleService.updateArticle(article , accessToken)), HttpStatus.OK);        }catch (Exception e){
            return ResponseEntity.internalServerError().body(
                    Response.error(500, "服务器内部错误")
            );
        }
    }

    //删除文章接口
    @DeleteMapping("delete/{articleId}")
    public ResponseEntity<?> deleteArticle(
            @PathVariable Integer articleId,
            @RequestHeader("Aut horization") String accessToken
    ) {
        try {
            articleService.deleteArticle(articleId, accessToken);
            return new ResponseEntity<>(Response.success("删除成功"), HttpStatus.OK);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(
                    Response.error(500, "服务器内部错误")
            );
        }
    }

    // 根据文章id获取文章
    @GetMapping("/{id}")
    public ResponseEntity<?> getArticleById(
            @PathVariable Integer id,
            @RequestHeader("Authorization") String accessToken
    ) {
        try {
            return new ResponseEntity<>(
                    Response.success("获取成功", articleService.getArticleById(id, accessToken)),
                    HttpStatus.OK
            );
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(
                    Response.error(500, "服务器内部错误")
            );
        }
    }



    //TODO 添加单个标签
    @PostMapping("/{id}/tags")
    public ResponseEntity<?> addTagToArticle(
            @PathVariable Integer id,
            @RequestBody org.example.ag_notes.entity.Tag tag,
            @RequestHeader("Authorization") String accessToken
    ) {
        try {
            articleService.addTagToArticle(id, tag.getName(), accessToken);
            return ResponseEntity.ok(
                    Response.success("标签添加成功")
            );
        } catch (AuthenticationException e) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
                    .body(Response.error(401, "无效或过期的Token"));
        } catch (Exception e) {
            return ResponseEntity.internalServerError()
                    .body(Response.error(500, "服务器内部错误"));
        }
    }


    @GetMapping("/{articleId}/tags")
    public ResponseEntity<Response<List<TagDTO>>> getArticleTags(
            @PathVariable Integer articleId,
            @RequestHeader("Authorization") String token
    ) {
        try {
            List<TagDTO> tags = articleService.getArticleTags(articleId, token);
            return ResponseEntity.ok(Response.success("获取成功", tags));
        } catch (AuthenticationException e) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
                    .body(Response.error(401, "无效或过期的Token"));
        } catch (Exception e) {
            return ResponseEntity.internalServerError()
                    .body(Response.error(500, "服务器内部错误"));
        }
    }
}
